package com.rc.saas.tenant.service.hlht;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.mapper.discount.TenantDiscountPolicyMapper;
import com.rc.saas.tenant.mapper.hlht.HlhtTenantOpenMapper;
import com.rc.saas.tenant.model.hlht.HlhtTenantOpen;
import com.rc.saas.tenant.model.hlht.HlhtTenantOpenExample;
import com.rc.saas.tenant.model.hlhtsk.*;
import com.rc.saas.tenant.search.hlht.HlhtTenantOpenSearch;
import com.rc.saas.tenant.service.hlhtsk.HlhtskBizTenantService;
import com.rc.saas.tenant.service.hlhtsk.HlhtskGovtTenantOperatorService;
import com.rc.saas.tenant.service.hlhtsk.HlhtskGovtTenantService;
import com.rc.saas.tenant.service.hlhtsk.HlhtskTenantService;
import com.rc.saas.tenant.vo.hlht.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HlhtTenantOpenServiceImpl implements HlhtTenantOpenService {

    @Autowired
    private HlhtTenantOpenMapper hlhtTenantOpenMapper;

    @Override
    public HlhtTenantOpenMapper getMapper() {
        return hlhtTenantOpenMapper;
    }

    @Autowired
    private HlhtskGovtTenantService hlhtskGovtTenantService;

    @Autowired
    private HlhtskGovtTenantOperatorService hlhtskGovtTenantOperatorService;

    @Autowired
    private HlhtskBizTenantService hlhtskBizTenantService;

    @Autowired
    private HlhtskTenantService hlhtskTenantService;

    @Autowired
    private TenantDiscountPolicyMapper tenantDiscountPolicyMapper;

    @Override
    public PageInfo<HlhtTenantOpenVo> listPageHlhtTenantOpen(HlhtTenantOpenSearch hlhtTenantOpenSearch) {
        String tenantCode = hlhtTenantOpenSearch.getTenantCode();
        PageHelper.startPage(hlhtTenantOpenSearch.getOffset(), hlhtTenantOpenSearch.getLimit());
        List<HlhtTenantOpenVo> hlhtTenantOpenList = hlhtTenantOpenMapper.listPageHlhtTenantOpen(hlhtTenantOpenSearch);
        return new PageInfo<HlhtTenantOpenVo>(hlhtTenantOpenList);
    }

    @Override
    public List<HlhtTenantOpenExportVo> listHlhtTenantOpen(HlhtTenantOpenSearch hlhtTenantOpenSearch) {
        List<HlhtTenantOpenExportVo> list = hlhtTenantOpenMapper.listHlhtTenantOpen(hlhtTenantOpenSearch);
        return list;
    }

    /**
     * 获取与政府互联互通时，返回分销商的密钥列表
     */
    @Override
    public List<HlhtskGovtTenantOperatorAndTenantOperatorName> listHlhtskGovtTenantOperator(String tenantCode, String hlhtOperatorId) {


        HlhtskGovtTenantOperatorAndTenantOperatorName example = new HlhtskGovtTenantOperatorAndTenantOperatorName();
        example.setTenantCode(tenantCode);
        example.setHlhtOperatorId(hlhtOperatorId);


        List<HlhtskGovtTenantOperatorAndTenantOperatorName> hlhtskGovtTenantOperatorAndTenantOperatorNames =
                hlhtskGovtTenantOperatorService.queryHlhtskGovtTenantOperatorAndTenantOperatorName(example);
        return hlhtskGovtTenantOperatorAndTenantOperatorNames;

    }

    /**
     * 分销商对政府时，返回运营商的密钥信息
     */
    @Override
    public HlhtskGovtTenant selectHlhtskGovtTenant(String tenantCode, String hlhtOperatorId) {
        HlhtskGovtTenantExample example = new HlhtskGovtTenantExample();
        example.createCriteria()
                .andTenantCodeEqualTo(tenantCode)
                .andHlhtOperatorIdEqualTo(hlhtOperatorId);
        List<HlhtskGovtTenant> hlhtskGovtTenantList = hlhtskGovtTenantService.selectByExample(example);
        if (hlhtskGovtTenantList.size() > 0) return hlhtskGovtTenantList.get(0);
        return null;
    }

    /**
     * 设备运营商对第三方商业运营商时，返回设备运营商的密钥信息
     */
    @Override
    public HlhtskBizTenant selectHlhtskBizTenant(String tenantCode, String hlhtOperatorId) {
        HlhtskBizTenantExample example = new HlhtskBizTenantExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andHlhtOperatorIdEqualTo(hlhtOperatorId);
        List<HlhtskBizTenant> hlhtskBizTenantList = hlhtskBizTenantService.selectByExample(example);
        if (hlhtskBizTenantList.size() > 0) return hlhtskBizTenantList.get(0);
        return null;
    }

    /**
     * 内部互联时,返回设备运营商的密钥信息
     *
     * @return
     */
    @Override
    public HlhtskTenant selectHlhtskTenant(String tenantCode, String hlhtTenantCode) {
        HlhtskTenantExample example = new HlhtskTenantExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andHlhtTenantCodeEqualTo(hlhtTenantCode);
        List<HlhtskTenant> hlhtskTenantList = hlhtskTenantService.selectByExample(example);
        if (hlhtskTenantList.size() > 0) return hlhtskTenantList.get(0);
        return null;
    }


    @Override
    public PageInfo<HlhtDiscountResultVo> listPageHlhtDiscount(BaseSearch baseSearch, String tenantCode, Long memberId) {
        PageHelper.startPage(baseSearch.getOffset(), baseSearch.getLimit());
        List<HlhtDiscountResultVo> hlhtDiscountResultVos = tenantDiscountPolicyMapper.listPageHlhtDiscount(tenantCode,memberId);
        return new PageInfo<HlhtDiscountResultVo>(hlhtDiscountResultVos);
    }

    @Override
    public void updateStatusByExpiredDate() {
        hlhtTenantOpenMapper.updateStatusByExpiredDate();
    }

    @Override
    public List<String> listOperatorIdByTenantCode(String tenantCode) {
        return hlhtTenantOpenMapper.listOperatorIdByTenantCode(tenantCode);
    }

    @Override
    public List<HlhtTenantVo> listHlhtTenantByTenantCode(String tenantCode) {
        return hlhtTenantOpenMapper.listHlhtTenantByTenantCode(tenantCode);
    }

    @Override
    public List<HlhtTenantVo> listByHlhtTenantCode(String hlhtTenantCode) {
        return hlhtTenantOpenMapper.listByHlhtTenantCode(hlhtTenantCode);
    }
}
